package cn.springbook.cim.config.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.springbook.cim.domain.common.AjaxJson;
import cn.springbook.cim.domain.common.Code;

@ControllerAdvice
public class GlobalExceptionHandler {

	
	@ExceptionHandler(MultipartException.class)
	@ResponseBody
    public AjaxJson handleError1(MultipartException ex, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		j.setObj(ex);
		j.setMessage(ex.getMessage());
		j.setCode(Code.INTERNALSERVERERROR.value());
		// 执行一些操作
		System.out.println("===================文件上传异常处理====================");
		return j;
    }
	
	// 处理异常
	@ExceptionHandler
	@ResponseBody
	public AjaxJson handleException(Exception ex) {
		AjaxJson j = new AjaxJson();
		j.setObj(ex);
		j.setMessage(ex.getMessage());
		j.setCode(Code.INTERNALSERVERERROR.value());
		// 执行一些操作
		System.out.println("===================异常处理器处理====================");
		return j;
	}
	
}
